#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
///////////////////////////////
//#include "ado/fun_sets.h"
//#include "standard/CThrowException.h"
#include "standard/DBConfig.h"
#include "ado/DbMysql.h"
#include "ado/Selects.h"
#include "standard/nstring.h"
#include "processlog.h"


Selects *Selects::_init = NULL;

Selects::Selects(void *db)
{
    m_db = (tDataBase*) db; 
     
	this->_classname.clear();
	this->_sort.clear();
	this->_limit.clear();
    
    std::string classname = m_db->classname;

    if (!(m_db->database).empty()) {
        m_dbname = m_db->database;
    }
    //std::string 
    this->_classname = tolower_str(classname);
	//GetFunInstance("tolower_str",this->_classname ,classname );

}

Selects::~Selects()
{
    m_db->database.clear();
    m_db->classname.clear();
}


Selects* Selects::init(void *db)
{
    _init = new Selects(db);
        
	return _init; 
}

Selects *Selects::getColumns()
{
    return this;
}

/**
 *  find(2, "order_id=? AND use_id=?", "887", "test")
 *  `order_id`='887' AND `cate_name`='test'
 */
Selects *Selects::find(const char *tokens, va_list vp)
{
    this->findByArray(tokens, vp); 
    return this;
}

Selects *Selects::limit(int num, ...)
{
    char *snum = NULL;
    va_list vp;
    va_start(vp, num);
    unsigned snum_len;
    
    snum = new char[sizeof(int)*2+1];
	memset(snum, 0, sizeof(int)*2+1);
	/*
    if (snum==NULL) 
        throw CThrowException("snum error", 0);
	*/
     
    switch (num) {
        case 0: break;
        case 1: snum_len = sprintf(snum, "%d", va_arg(vp, int)); break;
        case 2: snum_len = sprintf(snum, "%d,%d", va_arg(vp, int), va_arg(vp, int)); break;        
        default: snum_len = sprintf(snum, "%d,%d", va_arg(vp, int), va_arg(vp, int)); break;        
    }
    
    this->_limit = snum;
    
    if (snum != NULL) {
        delete [] snum;
        snum = NULL;
    }
    
    va_end(vp);
    return this;
}

Selects *Selects::order(std::string sort)
{
    this->_sort = sort;
    return this;
}

Selects *Selects::getOne()
{
    this->limit(1, 1);
    return this;
}

std::string Selects::createSql()
{    
    std::string qsql;
    
    qsql = "select * from " + m_dbname + "." + m_prefix + this->_classname;   

    if (this->getWhere().length()) qsql += " where " + this->getWhere();
    if (this->_sort.length()) qsql += " order by " + this->_sort;
    if (this->_limit.length()) qsql += " limit " + this->_limit;      
    
    return qsql;
}

std::map<std::string, std::string> Selects::query()
{   
    std::string qsql;
    std::map<std::string, std::string> qure;
    
    qsql    = this->createSql();
    qure = DbMysql::instance()->execute(qsql.c_str())->getRow();    
       
    return qure;
}

std::map<unsigned long int, std::map<std::string, std::string> >  Selects::querys()
{   
    std::string qsql = this->createSql();            
    
    return DbMysql::instance()->execute(qsql.c_str())->getArray();    
}
